<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;

class LiveController extends CommonController
{
    public function live_list() {

        $model=M('Live');
        $title = I('get.title', '');
        $userName= I('get.username', '');
        $map = array();
        if($title) {
            $map['title'] = array("like","%{$title}%");
        }
        if($userName) {
            $map['realname'] = array("like","%{$userName}%");
        }
        $total = $model->alias('live')->where($map)->group('group_id')->join('__USER_INFO__ u ON u.uid=live.apply_id','left')->select();
        $total=count($total);
        $page = new \Think\Page($total, 25);
        $show = $page->show();

        $liveList = $model
            ->where($map)
            ->alias('live')
            ->group('group_id')
            ->join('__USER_INFO__ u ON u.uid=live.apply_id','left')
            ->order('start_time DESC')
            ->limit($page->firstRow .',' . $page->listRows)
            ->select();

        $columeList=M('front_category')->where(array('status'=>1))->select();

        $this->assign('showpage', $show);
        $this->assign('liveList', $liveList);
        $this->assign('columeList', $columeList);
        $this->display();
    }

    public function  liveInfo(){
        $group_id=I('get.group_id');
        if($group_id){
            $model=M('Live');
            $liveList = $model
                ->alias('live')
                ->where(array('group_id'=>$group_id))
                ->join('__USER_INFO__ u ON u.uid=live.user_id','left')
                ->order('id ASC')
                ->select();
            $this->assign('liveList', $liveList);
            $this->display();
        }else{
            $this->error('参数错误' );
        }
    }


    public function ajaxStreamOff(){
        $live_id=I('get.liveId');
        $stream=I('get.stream');
        if($live_id&&$stream){
            $this->ajaxReturn(liveStatus(1, array(array('id'=>$live_id,'stream'=>$stream)),0));
        }
    }

    public function ajax_get_vstatus(){
        $id=I('get.id');
        if($id){
            echo M('Live')->getFieldById('v_status',$id);
        }else{
            echo -1;
        }
    }

    public function ajaxStreamOn(){
        $live_id=I('get.liveId');
        $stream=I('get.stream');
        if($live_id&&$stream){
            $this->ajaxReturn(liveStatus(2, array(array('id'=>$live_id,'stream'=>$stream)),1));
        }
    }

    public function ajaxLiveOn(){
        $live_id=I('get.liveId');
        $stream=I('get.stream');
        if($live_id&&$stream){
            $this->ajaxReturn(liveStatus(2, array(array('id'=>$live_id,'stream'=>$stream)),2));
        }
    }

    public function ajaxLiveOff(){
        $live_id=I('get.liveId');
        $stream=I('get.stream');
        if($live_id&&$stream){
            $this->ajaxReturn(liveStatus(1, array(array('id'=>$live_id,'stream'=>$stream)),3));
        }
    }

    public function showBTNs($id){
        if($id){
            $str='';
            $zStatus=M('Live')->getFieldById($id,'z_status');
            switch($zStatus){
                case 0:
                    $str="<button type=\"button\" class=\"stream streamOn btn-warning btn btn-sm\">开启直播通道</button>";
                    break;
                case 1:
                    $str="<button type=\"button\" class=\"stream streamOff btn-danger btn btn-sm\">关闭直播通道</button>";
                    break;
                case 2:
                    $str="<button type=\"button\" class=\"stream streamOff btn-warning btn btn-sm\">关闭直播通道</button>";
                    $str.="<button type=\"button\" class=\"stream liveOff btn-danger btn btn-sm\">点击结束直播</button>";
                    break;
            }
            echo $str;
        }
    }

    public function ajaxDelLive(){
        if(IS_GET) {
            $id = I('get.id');
        } elseif(IS_POST) {
            $id = I('post.id');
            is_array($id) && $id = implode(',', $id);
        }

        if(empty($id)) {
            $message['status'] = -1;
            $message['msg'] = '选项不能为空';
            $this->ajaxReturn($message);
        }

        $map['group_id'] = array('in', $id);

        $res = M('Live')->where($map)->delete();
        if($res) {
            $message['status'] = 1;
            $message['msg'] = '删除成功';
            $this->ajaxReturn($message);
        } else {
            $message['status'] = -1;
            $message['msg'] = '删除失败，请重试';
            $this->ajaxReturn($message);
        }
    }

    function verify($id, $state){
        $res = M('Live')->where(array('group_id' => $id))->save(array(
            'v_status' => $state,
        ));

        if($state==1){
            $mkHTML=new \Common\Util\makeHTML();
            $mkHTML-> ByGroupId($id);
        }

        if($res !== false) {
            $this->success('审核成功');
        } else {
            $this->error('审核失败');
        }
    }

    function test(){
        $mkHTML=new \Common\Util\makeHTML();
        $mkHTML-> ByGroupId('20160907153130_20_57cfc2521869c');
    }


    function ajaxIssueColumn(){
        $cid=I('get.cid');
        $gid=I('get.gid');
        if($cid && $gid){
            $col=M('Publish')->where(array('live_group_id'=>$gid,'category_id'=>$cid))->find();
            if($col){
                $mess['resultCode']=0;
                $mess['info']='已签发到此栏目';
            }else{
                $live=M('live')->where(array('group_id'=>$gid))->find();
                if($live){
                    $data['live_group_id']=$live['group_id'];
                    $data['category_id']=$cid;
                    $data['type']='live';
                    $data['update_time']=time();
                    $data['live_group_id']=$live['group_id'];
                    M()->startTrans();
                    $res=M('Publish')->add($data);
                    if($res){
                        $res1=M('Publish')->save(array('sort'=>$res,'id'=>$res));
                        if($res1===false){
                            $mess['resultCode']=1;
                            $mess['info']='签发失败';
                            M()->rollback();
                        }else{
                            $mess['resultCode']=0;
                            $mess['info']='签发成功';
                            M()->commit();
                        }
                    }else{
                        $mess['resultCode']=1;
                        $mess['info']='签发失败';
                        M()->rollback();
                    }
                }else{
                    $mess['resultCode']=1;
                    $mess['info']='直播不存在';
                }
            }
        }else{
            $mess['resultCode']=1;
            $mess['info']='参数错误';
        }
        $this->ajaxReturn($mess);
    }

}